package com.example.opengldemo._111_;

import android.content.Context;
import android.opengl.GLES10;
import android.opengl.GLES30;
import android.opengl.GLSurfaceView;
import android.view.MotionEvent;
import android.view.View;

import com.example.opengldemo.R;
import com.example.opengldemo.util.MatrixState;
import com.example.opengldemo.util.TextureUtil;

import java.util.Collections;

import javax.microedition.khronos.egl.EGLConfig;
import javax.microedition.khronos.opengles.GL10;

public class MyTreeView extends GLSurfaceView implements GLSurfaceView.Renderer  {

    TreeGroup treeGroup ;
    Desert desert ;

    public MyTreeView(Context context) {
        super(context);
        initConfig();
    }
    private void initConfig() {
        setEGLContextClientVersion(3);
        setRenderer(this);
        this.setRenderMode(GLSurfaceView.RENDERMODE_CONTINUOUSLY);
    }
    float x;
    float y;
    float Offset=15;
    float preX;
    float preY;
    boolean flag=true;
    static final float UNIT_SIZE=1f;
    static float direction=0;
    static float cx=0;
    static float cz=15;
    static final float DEGREE_SPAN=(float)(3.0/180.0f*Math.PI);
    @Override
    public boolean onTouchEvent(MotionEvent event)
    {
        x=event.getX();
        y=event.getY();
        switch(event.getAction())
        {
            case MotionEvent.ACTION_DOWN:
                flag=true;
                new Thread()
                {
                    @Override
                    public void run()
                    {
                        while(flag)
                        {
                            if(x>0&&x<WIDTH/2&&y>0&&y<HEIGHT/2)
                            {
                                Offset=Offset-0.5f;
                            }
                            else if(x>WIDTH/2&&x<WIDTH&&y>0&&y<HEIGHT/2)
                            {
                                Offset=Offset+0.5f;
                            }
                            else if(x>0&&x<WIDTH/2&&y>HEIGHT/2&&y<HEIGHT)
                            {
                                direction=direction+DEGREE_SPAN;
                            }
                            else if(x>WIDTH/2&&x<WIDTH&&y>HEIGHT/2&&y<HEIGHT)
                            {
                                direction=direction-DEGREE_SPAN;
                            }
                            try
                            {
                                Thread.sleep(100);
                            }
                            catch(Exception e)
                            {
                                e.printStackTrace();
                            }
                        }
                    }
                }.start();
                break;
            case MotionEvent.ACTION_UP:
                flag=false;
                break;
        }
        cx=(float)(Math.sin(direction)*Offset);
        cz=(float)(Math.cos(direction)*Offset);

        treeGroup.calculateBillboardDirection();
        Collections.sort(treeGroup.alist);

        MatrixState.setCamera(cx,0,cz,0,0,0,0,1,0);
        return true;
    }
    int WIDTH ;
    int HEIGHT ;
    @Override
    public void onSurfaceCreated(GL10 gl10, EGLConfig eglConfig) {
        GLES30.glClearColor(0.3f , 0.3f ,0.3f , 1.0f);
        desert = new Desert(this) ;
        desertTextureId = TextureUtil.getTextureId(this , R.drawable.was , null, false) ;
        MatrixState.setInitStack();
        WIDTH = getWidth() ;
        HEIGHT = getHeight() ;
    }
    int desertTextureId ;
    @Override
    public void onSurfaceChanged(GL10 gl10, int i, int i1) {
        GLES30.glViewport(0 , 0 , i , i1);
        float des = (float)i/i1 ;
        MatrixState.setProjectFrustum(-des , des  ,-1 , 1 , 5 , 50 );
        MatrixState.setCamera( 0 , 0 , 7 , 0 , 0  , 0 , 0 ,1, 0);
    }

    @Override
    public void onDrawFrame(GL10 gl10) {
        GLES30.glClear(GLES30.GL_DEPTH_BUFFER_BIT|GLES30.GL_COLOR_BUFFER_BIT);
        MatrixState.pushMatrix();
        MatrixState.translate(MatrixState.translate[0] , MatrixState.translate[1] ,MatrixState.translate[2] );
        desert.drawSelf(desertTextureId);
        MatrixState.popMatrix();
    }
}
